package com.yzt.zhuju.utils;

import android.graphics.Color;
import android.text.Html;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.TextPaint;
import android.text.TextUtils;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.text.style.ForegroundColorSpan;
import android.text.style.URLSpan;
import android.view.View;
import android.widget.TextView;

import com.yzt.zhuju.arouter.RouterMain;

import org.jetbrains.annotations.NotNull;

/**
 * author : bob
 * date   : 2022/12/2
 * desc   :
 */
public class SpannableUtils {

    public static void setTipClick(TextView tv_wena, String tip, int defaultClickColor, boolean isGoToWeb) {
        setTipClick(tv_wena, tip, defaultClickColor, isGoToWeb, false);
    }

    public static void setTipClick(TextView tv_wena, String tip, int defaultClickColor, boolean isGoToWeb, boolean isUnderLine) {
        try {
            if (tv_wena == null) {
                return;
            }
            if (TextUtils.isEmpty(tip)) {
                return;
            }
            tv_wena.setHighlightColor(tv_wena.getContext().getResources().getColor(android.R.color.transparent));
            Spanned spanned = Html.fromHtml(tip);
            SpannableStringBuilder builder = new SpannableStringBuilder(spanned);
            URLSpan[] spans = builder.getSpans(0, spanned.length(), URLSpan.class);
            for (URLSpan urlSpan : spans) {
                int spanStart = builder.getSpanStart(urlSpan);
                int spanEnd = builder.getSpanEnd(urlSpan);
                int spanFlags = builder.getSpanFlags(urlSpan);
                ForegroundColorSpan[] foregroundColorSpans = builder.getSpans(spanStart, spanEnd, ForegroundColorSpan.class);
                if (foregroundColorSpans == null || foregroundColorSpans.length == 0) {
                    foregroundColorSpans = new ForegroundColorSpan[1];
                    foregroundColorSpans[0] = new ForegroundColorSpan(defaultClickColor);
                }
                builder.setSpan(new MyClickSpan(urlSpan.getURL(), foregroundColorSpans[0], isGoToWeb, isUnderLine), spanStart, spanEnd, spanFlags);
                builder.removeSpan(urlSpan);
            }
            tv_wena.setText(builder);
            tv_wena.setMovementMethod(LinkMovementMethod.getInstance());
        } catch (Exception ignored) {

        }
    }

    private static class MyClickSpan extends ClickableSpan {
        private final String url;
        private final ForegroundColorSpan foregroundColorSpan;
        private final boolean isGoToWeb;
        private final boolean isUnderLine;

        public MyClickSpan(String url, ForegroundColorSpan foregroundColorSpan, boolean isGoToWeb, boolean isUnderLine) {
            this.url = url;
            this.foregroundColorSpan = foregroundColorSpan;
            this.isGoToWeb = isGoToWeb;
            this.isUnderLine = isUnderLine;
        }

        @Override
        public void updateDrawState(@NotNull TextPaint ds) {
            super.updateDrawState(ds);
            ds.setColor(foregroundColorSpan.getForegroundColor()); //设置颜色
            //去掉下划线，默认是带下划线的
            ds.setUnderlineText(isUnderLine);
        }

        @Override
        public void onClick(@NotNull View widget) {
            if (isGoToWeb) {
                RouterMain.goToWeb(url);
            } else {
                UrlUtil.turnToARoutePage4JO(url);
            }
        }
    }
}
